<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>deepGet</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const deepGet = (obj, keys) =>
            keys.reduce(
                (o, i) => (o && o[i] !== null && o[i] !== undefined ? o[i] : null),
                obj
            );
        let index = 2;
        const data = {
            foo: {
                foz: [1, 2, 3],
                bar: {
                    baz: ['a', 'b', 'c']
                }
            }
        };
        console.log(deepGet(data, ['foo', 'foz', index])); // get 3
        console.log(deepGet(data, ['foo', 'bar', 'baz', 8, 'foz'])); // null
    </script>
</body>

</html>